package kr.co.sdsway2.service;

import java.util.List;

import javax.annotation.Resource;

import kr.co.sdsway2.common.AbstractService;
import kr.co.sdsway2.vo.PtInfVO;

import org.springframework.stereotype.Service;

@Service
public class PtInfBeanService extends AbstractService {

	@Resource(name="ptInfBeanList")
	private List<PtInfVO> ptInfBeanList;

	/** 새로운 PT 입력 */
	public int insertPtInfBean(Integer ptId,String ptStatCd,Integer pstPollId) {
		if(getIndex(ptId)<0){//없을때만 insert
			PtInfVO vo = new PtInfVO();
			vo.setPtId(ptId);
			vo.setPtStatCd(ptStatCd);
			vo.setPstPollId(pstPollId);
			ptInfBeanList.add(vo);
			return 1;	//성공
		}else{
			return 0;	//실패
		}
	}
	
	/** 새로운 PT 입력 */
	public PtInfVO insertPtInfBean(Integer ptId,String ptStatCd,String ptName) {
		if(getIndex(ptId)<0){//없을때만 insert
			PtInfVO vo = new PtInfVO();
			vo.setPtId(ptId);
			vo.setPtStatCd(ptStatCd);
			vo.setPtName(ptName);
			ptInfBeanList.add(vo);
			return vo;	//성공
		}else{
			return null;	//실패
		}
	}

	/** 프리젠테이션 삭제(by ID) 서비스 */
	public void remove(Integer ptId) {
		int idx = getIndex(ptId);
		if( idx != -1 ) this.ptInfBeanList.remove(idx);
	}

	/** 프리젠테이션 삭제(by index) 서비스 */
	public void remove(int idx){
		this.ptInfBeanList.remove(idx);
	}

	/** 프리젠테이션 목록 조회 서비스 */
	public List<PtInfVO> getList(){
		return this.ptInfBeanList;
	}

	/** 프리젠테이션 조회 서비스 */
	public PtInfVO getPtInfBean(Integer ptId) {
		int i = getIndex(ptId);
		if(i>=0){
			return ptInfBeanList.get(i);
		}
		return null;
	}

	/** 프리젠테이션 상태 변경 서비스 */
	public void update(Integer ptId,String ptStatCd,Integer pstPollId){
		PtInfVO vo = null;
		int i = getIndex(ptId);
		if(i<0){
			insertPtInfBean(ptId, ptStatCd, pstPollId);
			i = getIndex(ptId);
		}
		if(i>=0){
			vo = ptInfBeanList.get(i);
			if(ptStatCd!=null) vo.setPtStatCd(ptStatCd);
			if(pstPollId!=null) vo.setPstPollId(pstPollId);
		}
	}

	/** 아바타ID를 통해 index 획득 */
	private int getIndex(Integer ptId){
		int idx = -1;
		int size = ptInfBeanList.size();
		PtInfVO vo;
		for(int i=0;i<size;i++){
			vo = ptInfBeanList.get(i);
			if(vo!=null && vo.getPtId()!=null){
				if(vo.getPtId().equals(ptId))
					idx = i;
			}
		}
		return idx;
	}
	
}
